/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.vehiculo.controller;

import com.vehiculo.DAO.VehiculoDAO;
import com.vehiculo.Entity.Cambio_Aceite;
import com.vehiculo.Entity.Vehiculo;
import com.vehiculo.TO.VehiculoTO;
import com.vehiculo.helper.Conversiones;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Daniel
 */
public class ControladorBasico extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    private VehiculoDAO VehDAO;
    
    @Override
    public void destroy(){
        this.VehDAO.closeEntityMAnager();
                
    }
    @Override
    public void init()throws ServletException{
        this.VehDAO = new VehiculoDAO();
    }
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String operacion=null;
        try{
        operacion = request.getParameter("op");
        }catch(Exception e){
            
        }
        
        if(operacion==null){
            response.sendRedirect("inicio.jsp");
        }
        else if(operacion.equalsIgnoreCase("1")){
            response.sendRedirect("inicio.jsp");
        }else if(operacion.equalsIgnoreCase("2")){
            List<Cambio_Aceite> listCambio_Aceite = VehDAO.getListaCambio_Aceite();
            System.out.println("size"+listCambio_Aceite.size());
            request.setAttribute("cambio_aceite", listCambio_Aceite);
            RequestDispatcher rd = request.getRequestDispatcher("/insertar.jsp");
            rd.forward(request, response);
            
        }else if(operacion.equalsIgnoreCase("3")){
            List<Cambio_Aceite> listCambio_Aceite = VehDAO.getListaCambio_Aceite();
            System.out.println("size"+listCambio_Aceite.size());
            request.setAttribute("cambio_aceite", listCambio_Aceite);
            RequestDispatcher rd = request.getRequestDispatcher("/listarXCambio_Aceite.jsp");
            rd.forward(request, response);
            
        }else if(operacion.equalsIgnoreCase("guardar")){
            Vehiculo t = new Vehiculo();
            String codigo_vehiculo = request.getParameter("codigo_vehiculo_vehiculo");
            String marca = request.getParameter("marca");
            String color = request.getParameter("color");
            String fkcambio_aceite = request.getParameter("slcCambio_Aceite");
            if(this.VehDAO.insertarVehiculo(
                Conversiones.convertirVehiculorTO_to_vehiculo(
                    new VehiculoTO(codigo_vehiculo, marca, color, fkcambio_aceite))))
                response.sendRedirect("exito.jsp");
            else
                response.sendRedirect("exito.jsp");
        }else if(operacion.equalsIgnoreCase("listarxC")){
            List<Cambio_Aceite> listCambio_Aceite = VehDAO.getListaCambio_Aceite();
            System.out.println("size"+listCambio_Aceite.size());
            request.setAttribute("cambio_aceite", listCambio_Aceite);
            
            Vehiculo t = new Vehiculo();
            String fkcambio_aceite = request.getParameter("slcCambio_Aceite");
            List<Vehiculo> lista = VehDAO.getCambio_Aceite(fkcambio_aceite);
            System.out.println("tam1"+lista.size());
            List<VehiculoTO> listaTO = Conversiones.convertirListaVehiculo_ListaVehiculoTO(lista);
            System.out.println("tam"+listaTO.size());
            request.setAttribute("vehiculo", listaTO);
            RequestDispatcher rd = request.getRequestDispatcher("/listarXCambio_Aceite.jsp");
            rd.forward(request, response);
            
        }else if(operacion.equalsIgnoreCase("4")){
            List<Vehiculo> lista = VehDAO.listarVehiculo();
            List<VehiculoTO> listaTO = Conversiones.convertirListaVehiculo_ListaVehiculoTO(lista);
            request.setAttribute("vehiculo", listaTO);
            RequestDispatcher rd = request.getRequestDispatcher("/listar.jsp");
            rd.forward(request, response);
            
        }else if(operacion.equalsIgnoreCase("eliminar")){
            String codigo_vehiculo = request.getParameter("codigo_vehiculo");
            if(this.VehDAO.eliminarVehiculo(codigo_vehiculo))
                response.sendRedirect("exito.jsp");
            else
                response.sendRedirect("exito.jsp");
        }
        
    }

    public VehiculoDAO getTrabDAO() {
        return VehDAO;
    }

    public void setTrabDAO(VehiculoDAO VehDAO) {
        this.VehDAO = VehDAO;
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
